package com.example.administrator.onepagelibrary.view.fragment;


import android.graphics.Color;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Keep;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;


import com.alipay.sdk.app.PayTask;
import com.example.administrator.commonlibrary.bean.MyBean;
import com.example.administrator.commonlibrary.bean.PayResult;
import com.example.administrator.commonlibrary.bese.BaseAppliction;
import com.example.administrator.commonlibrary.bese.BaseFragment;
import com.example.administrator.commonlibrary.contract.fragment.one.OneFragmentContract;
import com.example.administrator.commonlibrary.utils.LogUtil;
import com.example.administrator.commonlibrary.utils.ToastUtils;
import com.example.administrator.commonlibrary.utils.arouter.ARouterUtils;
import com.example.administrator.commonlibrary.welcomepage.MainActivity;
import com.example.administrator.commonlibrary.wholehelper.thirdparty.UShareHelper;
import com.example.administrator.onepagelibrary.R;
import com.example.administrator.onepagelibrary.R2;
import com.example.administrator.onepagelibrary.dagger.module.ViewParameterModule;
import com.example.administrator.onepagelibrary.onebase.OneBaseViewFragment;

import com.example.administrator.onepagelibrary.presenter.fragment.OneFragmentPresenter;
import com.example.administrator.onepagelibrary.view.activity.HuadongActivity;
import com.flyco.tablayout.CommonTabLayout;
import com.flyco.tablayout.SegmentTabLayout;
import com.flyco.tablayout.listener.CustomTabEntity;
import com.umeng.socialize.bean.SHARE_MEDIA;

import java.util.Map;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.OnClick;

import static com.example.administrator.commonlibrary.utils.ActivityUtils.newIntent;

@Keep
public class OneFragment extends OneBaseViewFragment<MainActivity, OneFragmentContract.Presenter> implements OneFragmentContract.View {
    @Inject
    OneFragmentPresenter presenter;
    @Inject
    MyBean myBean;
    @BindView(R2.id.one_content)
    Button button;
    @BindView(R2.id.tabLayout)
    SegmentTabLayout tabLayout;
    @Override
    protected OneFragmentContract.Presenter initPresenter() {
        return presenter;
    }

    @Override
    protected int onMLayout() {
        LogUtil.d("One---onCreterView");
        return R.layout.one_fragment;
    }

    @Override
    protected void onHidden(boolean only) {
        LogUtil.d("One---onHidden--"+only);
    }

    @Override
    protected void onMView(View view) {
        String[] name=new String[]{"你好","不好","可以","是吧"};
        tabLayout.setTabData(name);
    }

    @Override
    protected String setPageTitle() {
        button.setTextColor(Color.RED);
        return "首页";
    }


    public static BaseFragment newInstance() {
        return new OneFragment();
    }


    @OnClick({R2.id.one_content, R2.id.one_intent, R2.id.one_share, R2.id.one_qq_login, R2.id.one_pay, R2.id.dagger,R2.id.loading})
    public void onViewClicked(View view) {
        int id = view.getId();
        if (id == R.id.one_content) {
            newIntent(HuadongActivity.class).startActivity(getMyActivity());
        } else if (id == R.id.one_intent) {
            ARouterUtils.startActivity(getActivity(), "/two/mintent");
        } else if (id == R.id.one_share) {
            UShareHelper.shareQQImage(getActivity(), R.drawable.ic_launcher);
        } else if (id == R.id.one_qq_login) {
            UShareHelper.UQQLogin(getActivity(), SHARE_MEDIA.QQ);
        } else if (id == R.id.one_pay) {
            final String orderInfo = "app_id=2018051560133634&biz_content=%7B%22out_trade_no%22%3A%22201809111930377732948%22%2C%22total_amount%22%3A128.0%2C%22subject%22%3A%22%E9%9D%92%E8%8A%B1%E9%B1%BC%28%E5%8C%97%E4%BA%AC%29%E7%BD%91%E7%BB%9C%E7%A7%91%E6%8A%80%E6%9C%89%E9%99%90%E5%85%AC%E5%8F%B8%22%2C%22product_code%22%3A%22QUICK_MSECURITY_PAY%22%2C%22passback_params%22%3A%22null%22%7D&charset=utf-8&method=alipay.trade.app.pay&notify_url=http%3A%2F%2F192.168.0.100%3A8081%2Fqinghuayu-api%2Fpay%2FcallBack%2F&sign_type=RSA2&timestamp=2018-09-11%2019%3A30%3A37&version=1.0&sign=5cwZUbUo650SrM3S1dcaKZuVfaOMXmkbrlFmMrUAfEmfAHwgguBbptRvBPrpXxkV3HzcDjf9slfBgT1ZTwyJY2WrUZ6PX4MuI%2BL9I3zoJ7SvcYLotTwbyiwC%2FE0EjxorM9BM7NOePTjMfLF8CHFhbohOg%2FJitCwpFE6QxVSDFfp2AskhWb7HjSS1dGOB8Xb3Ej7rgBNmjKAx0DScglZyZgctI4EfSMCbvlsbZsl%2BT4vMO47dBj2q%2Bj3VDaKtTuFRVG00fLUdiAU9q8axoKDUeXJQEvuY7aWJjX2GZ%2Ftvy77Qj%2FhoIPw5r3XQ%2FBatPquo94PHNZ8GQnCF1bapSXtY7g%3D%3D";
            Runnable payRunnable = new Runnable() {
                @Override
                public void run() {
                    PayTask alipay = new PayTask(getActivity());
                    Map<String, String> result = alipay.payV2(orderInfo, true);
                    Message msg = new Message();
                    msg.what = 1;
                    msg.obj = result;
                    mHandler.sendMessage(msg);
                }
            };
            // 必须异步调用
            Thread payThread = new Thread(payRunnable);
            payThread.start();
        } else if (id == R.id.dagger) {
            daggerInject(new ViewParameterModule("成功", "失败"));
           ToastUtils.bugToast(myBean.getMessage() + "--" + myBean.getErrorCode());
        }else if (id == R.id.loading){
             showLoadingDialog();
        }

    }

    @Override
    public void setToast() {
        ToastUtils.bugToast("Rxjava发送过来的消息--");
    }

    private Handler mHandler = new Handler() {
        public void handleMessage(Message msg) {
            PayResult payResult = new PayResult((Map<String, String>) msg.obj);
            String resultStatus = payResult.getResultStatus();
            // 判断resultStatus 为9000则代表支付成功
            if (TextUtils.equals(resultStatus, "9000")) {
                // 该笔订单是否真实支付成功，需要依赖服务端的异步通知。
                ToastUtils.bugToast("支付成功");
            } else {
                // 该笔订单真实的支付结果，需要依赖服务端的异步通知。
                ToastUtils.bugToast("支付失败");
            }

        }

        ;
    };

}
